package com.sunmouse.thread.download;

import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import com.sunmouse.util.FileUtil;
import com.sunmouse.util.PrintUtil;

public class MultiThreadExe {
	public static void main(String[] args) {
		String remote = "http://audio.vsaker.com/ysecret/secret-1.mp4";
		String local = "D://image/multi_123.mp4";
		long readSize = 5 * 4 * DownloadThread.READ_SIZE;// 5M
		File file = FileUtil.newFile(local);
		try {
			URL url = new URL(remote);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			if (conn.getResponseCode() == 200) {
				int length = conn.getContentLength();
				PrintUtil.pln("--length=" + length);
				if (length == -1)
					return;
				double cpd = (double) length / readSize;
				int cpi = (int) (length / readSize);
				int count = cpd > cpi ? cpi + 1 : cpi;
				PrintUtil.pln("---count="+count);
				for (int i = 0; i < count; i++) {//calculate num of threads
					new DownloadThread(i*readSize, readSize, url, file).start();
				}
				//PrintUtils.pln("download successfully");
				conn.disconnect();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
